#include "ciyouplayer.h"
#include "ui_ciyouplayer.h"
#include <QGraphicsDropShadowEffect>
#include <QMouseEvent>

CiYouPlayer::CiYouPlayer(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CiYouPlayer)
{
    ui->setupUi(this);
    initUI();
    connectSignalAndSlot();
}

CiYouPlayer::~CiYouPlayer()
{
    delete ui;
}

void CiYouPlayer::onSwitchPage(int pageId)
{
    ui->stackedWidget->setCurrentIndex(pageId);
    resetSwitchBtnInfo(pageId);
}

void CiYouPlayer::initUI()
{
    //去除窗口的边框
    setWindowFlag(Qt::FramelessWindowHint);

    //设置窗口透明
    setAttribute(Qt::WA_TranslucentBackground);

    //设置窗口图标
    setWindowIcon(QIcon(":/Image/homePage/logo.png"));

    //窗口添加阴影效果
    QGraphicsDropShadowEffect* dropShadow=new QGraphicsDropShadowEffect(this);
    dropShadow->setColor(Qt::black);
    dropShadow->setBlurRadius(5);
    dropShadow->setOffset(0,0);

    //直接给主窗口添加会产生一些错误
    //this->setGraphicsEffect(dropShadow);
    ui->CYPlayerBg->setGraphicsEffect(dropShadow);

    //设置页面切换按钮的文本和图片
    ui->homePageBtn->setTextColor("#000000");
    ui->homePageBtn->setImageAndText(":/Image/homePage/shouye.png","首页",HomePage);
    ui->myPageBtn->setTextColor("#999999");
    ui->myPageBtn->setImageAndText(":/Image/homePage/wodeno.png","我的",MyselfPage);
    ui->sysPageBtn->setTextColor("#999999");
    ui->sysPageBtn->setImageAndText(":/Image/homePage/adminno.png","系统",AdminPage);
}

void CiYouPlayer::connectSignalAndSlot()
{
    connect(ui->minBtn,&QPushButton::clicked,this,&QWidget::showMinimized);
    connect(ui->quitBtn,&QPushButton::clicked,this,&QWidget::close);

    connect(ui->homePageBtn,&PageSwitchButton::switchPage,this,&CiYouPlayer::onSwitchPage);
    connect(ui->myPageBtn,&PageSwitchButton::switchPage,this,&CiYouPlayer::onSwitchPage);
    connect(ui->sysPageBtn,&PageSwitchButton::switchPage,this,&CiYouPlayer::onSwitchPage);
}

void CiYouPlayer::resetSwitchBtnInfo(int pageId)
{
    //重新设置bodyLeft中按钮上的文本和图片的高亮
    //设置文本颜色
    QList<PageSwitchButton*> switchBtns=findChildren<PageSwitchButton*>();
    for(auto& switchBtn :switchBtns)
    {
        if(switchBtn->getPageId()!=pageId)
        {
            switchBtn->setTextColor("#999999");
        }
    }

    //设置按钮的图片
    if(pageId==HomePage)
    {
        //将HomePageBtn的图片设置为高亮
        //将MyselfPageBtn,AdminPageBtn的图片设置为灰色
        ui->homePageBtn->setImage(":/Image/homePage/shouye.png");
        ui->myPageBtn->setImage(":/Image/homePage/wodeno.png");
        ui->sysPageBtn->setImage(":/Image/homePage/adminno.png");
    }
    else if(pageId==MyselfPage)
    {
        ui->homePageBtn->setImage(":/Image/homePage/shouyeno.png");
        ui->myPageBtn->setImage(":/Image/homePage/wode.png");
        ui->sysPageBtn->setImage(":/Image/homePage/adminno.png");
    }
    else if(pageId==AdminPage)
    {
        ui->homePageBtn->setImage(":/Image/homePage/shouyeno.png");
        ui->myPageBtn->setImage(":/Image/homePage/wodeno.png");
        ui->sysPageBtn->setImage(":/Image/homePage/admin.png");
    }
    else
    {
        qDebug()<<"暂不支持的页面";
    }
}

void CiYouPlayer::mousePressEvent(QMouseEvent *event)
{
    //先要获取鼠标点击时的位置
    QPoint point=event->position().toPoint();

    //检测该位置是否在head中，且是鼠标左键单击时
    if(ui->head->geometry().contains(point))
    {
        if(event->button()==Qt::LeftButton)
        {
            dragPos=event->globalPosition().toPoint()-geometry().topLeft();
            return;
        }
    }

    QWidget::mousePressEvent(event);
}

void CiYouPlayer::mouseMoveEvent(QMouseEvent *event)
{
    QPoint point=event->position().toPoint();
    if(ui->head->geometry().contains(point))
    {
        if(event->buttons()==Qt::LeftButton)
        {
            move(event->globalPosition().toPoint()-dragPos);
            return;
        }
    }
    QWidget::mouseMoveEvent(event);
}
